// user/pages/invoce/add.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dutyParagraph: '',
    more: false,
    type: true,
    companyName: '',
    email: '',
    phone:'',
    address:'',
    bankName: '',
    oidList:[],
    addHead: false,
    total: 0,
    addType: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      oidList: JSON.parse(options.oidList),
      total: options.total,
      addType: options.type
    })
    this.selectHead()
  },
  moreBind(){
    this.setData({
      more: !this.data.more
    })
  },
  changeType(e){
    console.log(e)
    let type = e.currentTarget.dataset.type
    this.setData({
      type: type
    })
  },
  // inputSet(e){
  //   console.log(e)
  // },
  addForm(){
    
    let companyName = this.data.companyName 
    
    let email = this.data.email 
    let oidList  = this.data.oidList 
    let phone = this.data.phone 
    let address =this.data.address 
    let bankName =this.data.bankName 
    let accountNumber =this.data.accountNumber
    let type = this.data.type
    var dutyParagraph =''
    if(type==1){
      dutyParagraph = this.data.dutyParagraph 
      if(!companyName){this.message('请输入发票抬头')
       return}
      //  if(!dutyParagraph){this.message('请输入税号')
      //  return}
    }else{
      if(!companyName){this.message('请输入姓名')
       return}
      //  if(dutyParagraph){dutyParagraph =''}
    }
    var emailReg = /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/
    if(!emailReg.test(email)){
      this.message('请输入正确的邮箱号')
       return
    }
    if(!/^1[3-9]\d{9}$/.test(phone)){
      this.message('请输入正确的手机号')
       return
    }
    let that  =this
    let url = this.data.addType =="combo"? '/invoicing/personal/package/apply' : '/invoicing/add'
    let postData ={companyName, dutyParagraph, accountNumber, email, phone, oidList, accountNumber, address, bankName}
    service('POST', url, postData).then((res) => {
      if(res.data.code==0){
        if(that.data.addHead){
          that.addHeads({companyName, dutyParagraph, accountNumber, email, phone, accountNumber, address, bankName})
        }
        
        console.log('提交返回', res)
        wx.redirectTo({
          url: '/user/pages/invoce/history',
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  addHeads(postData){
    service('POST', '/invoicing/company/template', postData).then((res) => {
      // if(res.data.code==0){
      //   wx.navigateBack()
      // }else{
      // }
      
    })
  },
  //使用抬头模板
  useHead() {
    let that = this
    wx.navigateTo({
      url: '/user/pages/invoce/usehead',
      events:{
				onSelect(res){
          console.log(res);
          if(!res || res == 'undefine'){
           wx.showToast({
             title: '选择失败',
             icon: 'none'
           })
          }else{
            that.fetchData(res)
          }
					
				}
			}
    })
  },
  fetchData(id){
    let this_ = this
    service('POST', `/invoicing/info/template/${id}`).then((res) => {
      let data = res.data.template
      if(data.id){
        let type = data.dutyParagraph ? 1: 2
          this_.setData({
            companyName: data.companyName,
            dutyParagraph: data.dutyParagraph,
            email: data.email,
            phone: data.phone,
            type: type
         })
      }
      
    })
  },

  message(msg){
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },
  checkAll(){
    this.setData({
      addHead: !this.data.addHead
    })
  },
  selectHead(){
    let this_ = this
    service("POST","/invoicing/list/template").then(res => {
      if(res.data.code ==0){
        if(res.data.templates.length<1){
          this_.setData({
            addHead: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})